/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package genericos;
import java.util.Random;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.mail.internet.MimeUtility;
/**
 * Clase que genera un password en automatico para un cliente registrado
 * @author Fernando Ramírez Martínez <Servicios Disaster Recovery>
 * @version 1.0
 */
public class JSPassGenerator {

    private static final String DIGITS = "0123456789";
    private static final String LOCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
    private static final String UPCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String PRINTABLE_CHARACTERS = DIGITS + LOCASE_CHARACTERS + UPCASE_CHARACTERS;
    private final static String keyBuffer = "56af65d2";
    public static String generate(int passLength) {
        String retPass = null;
        Random m_generator = new Random();
        char[] availableChars = PRINTABLE_CHARACTERS.toCharArray();
        int availableCharsLeft = availableChars.length;
        StringBuffer temp = new StringBuffer(passLength);
        for (int i = 0; i < passLength; i++) {
            int pos = (int) (availableCharsLeft * m_generator.nextDouble());
            temp.append(availableChars[pos]);
            availableChars[pos] = availableChars[availableCharsLeft - 1];
            --availableCharsLeft;
        }
        retPass = String.valueOf(temp);

        return retPass;
    }
    

private static byte[] encode(byte[] b) throws Exception {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

OutputStream b64os = MimeUtility.encode(baos, "base64");

b64os.write(b);

b64os.close();

return baos.toByteArray();

}

private static byte[] decode(byte[] b) throws Exception {

ByteArrayInputStream bais = new ByteArrayInputStream(b);

InputStream b64is = MimeUtility.decode(bais, "base64");

byte[] tmp = new byte[b.length];

int n = b64is.read(tmp);

byte[] res = new byte[n];

System.arraycopy(tmp, 0, res, 0, n);

return res;

}

private static SecretKeySpec getKey() {

// keyBuffer = keyBuffer.substring(0, 8);

SecretKeySpec key = new SecretKeySpec(keyBuffer.getBytes(), "DES");

return key;

}
/**
 * Metodo que desencripta una cadena previamente encriptada por esta clase
 * @param s Cadena a Desencriptar
 * @return Cadena sin encriptacion
 * @throws Exception 
 */
public static String desencripta(String s) throws Exception {

String s1 = null;

if (s.indexOf("{DES}") != -1) {

String s2 = s.substring("{DES}".length());

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

cipher.init(2, getKey());

byte abyte0[] = cipher.doFinal(decode(s2.getBytes()));

s1 = new String(abyte0);

} else {

s1 = s;

}

return s1;

}
/**
 * Metodo que encripta una cadena para ocultar la informacion dada de un cliente
 * @param s Cadena a encriptar
 * @return Cadena encriptada
 * @throws Exception 
 */
public static String encripta(String s) throws Exception {

byte abyte0[];

SecureRandom securerandom = new SecureRandom();

securerandom.nextBytes(keyBuffer.getBytes());

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

cipher.init(1, getKey());

abyte0 = encode(cipher.doFinal(s.getBytes())); // antes

return "{DES}" + new String(abyte0);

}
  
}
